//
//  LJJTimeViewController.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/14.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJTimeViewController.h"
#import "LJJTimeSingleTableView.h"
#import "LJJTimeTwoButtonView.h"
#import <SDCycleScrollView.h>
#import "LJJSingleRootModel.h"
#import "LJJGroupRootModel.h"
#import "LJJGoodsDetailViewController.h"
#import "LJJSearchGoodsViewController.h"

@interface LJJTimeViewController () <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollView;    /** 最底层的滚动视图 **/
@property (strong, nonatomic) SDCycleScrollView *headImageView;    /** 轮播图 **/
@property (strong, nonatomic) LJJTimeTwoButtonView *twoButtonView;    /** 中间切换视图的view **/
@property (strong, nonatomic) LJJTimeSingleTableView *singleTableView;    /** 单品限购 **/
@property (strong, nonatomic) LJJTimeSingleTableView *groupTableView;    /** 品牌团购 **/
@property (strong, nonatomic) UISwipeGestureRecognizer *leftSwipe;    /** 左滑 **/
@property (strong, nonatomic) UISwipeGestureRecognizer *rightSwipe;    /** 右滑 **/
@property (strong, nonatomic) NSArray *singleModelArray;    /** 单品的数据源 **/
@property (strong, nonatomic) NSArray *groupModelArray;    /** 团购的数据源 **/

@end

@implementation LJJTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSearchBar];
    [self configUI];
    [self getHeadImageData];
    [self getSingleData];
    [self getGroupData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark -
#pragma mark - method
- (void)addSearchBar
{
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setImage:[UIImage imageNamed:@"限时特卖界面搜索按钮"] forState:UIControlStateNormal];
    searchButton.frame = CGRectMake(0, 0, 35, 35);
    [searchButton addTarget:self action:@selector(pushSearchViewControllerMethod:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    self.navigationItem.rightBarButtonItem = item;
}
- (void)pushSearchViewControllerMethod:(UIButton *)sender
{
    LJJSearchGoodsViewController *searchVC = [[LJJSearchGoodsViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}
- (void)configUI
{
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.headImageView];
    [self.scrollView addSubview:self.singleTableView];
    [self.scrollView addSubview:self.groupTableView];
    [self.scrollView addSubview:self.twoButtonView];
}
- (void)changeTableViewFrameWithButtonTag:(NSInteger)tag
{
    if (tag == 1000) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect singleFrame = self.singleTableView.frame;
            singleFrame.origin.x = 0;
            self.singleTableView.frame = singleFrame;
            CGRect groupFrame = self.groupTableView.frame;
            groupFrame.origin.x = WIDTH;
            self.groupTableView.frame = groupFrame;
        }];
    }
    else
    {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect singleFrame = self.singleTableView.frame;
            singleFrame.origin.x = -WIDTH;
            self.singleTableView.frame = singleFrame;
            CGRect groupFrame = self.groupTableView.frame;
            groupFrame.origin.x = 0;
            self.groupTableView.frame = groupFrame;
        }];
    }
}
- (void)getHeadImageData
{
    [self getData:@"appHome/appHome.do" param:nil success:^(id responseObject) {
        NSMutableArray *imageArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            NSString *imageURL = dic[@"ImgView"];
            [imageArray addObject:imageURL];
        }
        self.headImageView.imageURLStringsGroup = imageArray;
    } error:^(NSError *error) {
        LJJLog(@"headViewImageError: --- %@", error);
    }];
}
- (void)getSingleData
{
    [self getData:@"appActivity/appHomeGoodsList.do" param:nil success:^(id responseObject) {
        self.singleModelArray = [LJJSingleRootModel mj_objectArrayWithKeyValuesArray:responseObject];
        self.singleTableView.singleArray = self.singleModelArray;
        CGRect tableViewRect = self.singleTableView.frame;
        tableViewRect.size.height = self.singleModelArray.count * 170;
        self.singleTableView.frame = tableViewRect;
        if (self.twoButtonView.singleButton.selected) {
            self.scrollView.contentSize = CGSizeMake(0, self.singleModelArray.count * 170 + 280);
        }
        [self.singleTableView reloadData];
    } error:^(NSError *error) {
        LJJLog(@"singleTableError: --- %@", error);
    }];
}
- (void)getGroupData
{
    [self getData:@"appActivity/appActivityList.do" param:nil success:^(id responseObject) {
        self.groupModelArray = [LJJGroupRootModel mj_objectArrayWithKeyValuesArray:responseObject];
        self.groupTableView.groupArray = self.groupModelArray;
        CGRect tableViewRect = self.groupTableView.frame;
        tableViewRect.size.height = self.groupModelArray.count * 200;
        self.groupTableView.frame = tableViewRect;
        if (self.twoButtonView.groupButton.selected) {
            self.scrollView.contentSize = CGSizeMake(0, self.groupModelArray.count * 200 + 280);
        }
        [self.groupTableView reloadData];
    } error:^(NSError *error) {
        LJJLog(@"groupTableError: --- %@", error);
    }];
}
#pragma mark -
#pragma mark - scrollView delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    if (y > 230) {
        CGRect rect = self.twoButtonView.frame;
        rect.origin.y = y;
        self.twoButtonView.frame = rect;
    }
    else
    {
        CGRect rect = self.twoButtonView.frame;
        rect.origin.y = 230;
        self.twoButtonView.frame = rect;
    }
}
#pragma mark -
#pragma mark - lazy method
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49)];
        _scrollView.contentSize = CGSizeMake(0, 1980);
        _scrollView.delegate = self;
    }
    return _scrollView;
}
- (SDCycleScrollView *)headImageView
{
    if (!_headImageView) {
        _headImageView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH, 230) delegate:nil placeholderImage:[UIImage imageNamed:@""]];
        _headImageView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _headImageView.currentPageDotColor = [UIColor whiteColor];
    }
    return _headImageView;
}
- (LJJTimeTwoButtonView *)twoButtonView
{
    if (!_twoButtonView) {
        _twoButtonView = [[LJJTimeTwoButtonView alloc] initWithFrame:CGRectMake(0, 230, WIDTH, 50)];
        __weak typeof(self) weakSelf = self;
        _twoButtonView.buttonBlock = ^(NSInteger tag){
            [weakSelf changeTableViewFrameWithButtonTag:tag];
        };
    }
    return _twoButtonView;
}
- (LJJTimeSingleTableView *)singleTableView
{
    if (!_singleTableView) {
        _singleTableView = [[LJJTimeSingleTableView alloc] initWithFrame:CGRectMake(0, 280, WIDTH, 1700) style:UITableViewStylePlain];
        _singleTableView.isSingle = YES;
        __weak typeof(self) weakSelf = self;
        _singleTableView.goodsIDBlock = ^(NSString *goodsID, NSString *countryImage){
            LJJGoodsDetailViewController *detailVC = [[LJJGoodsDetailViewController alloc] init];
            detailVC.detailGoodsID = goodsID;
            detailVC.countryImage = countryImage;
            [weakSelf.navigationController pushViewController:detailVC animated:YES];
        };
    }
    return _singleTableView;
}
- (LJJTimeSingleTableView *)groupTableView
{
    if (!_groupTableView) {
        _groupTableView = [[LJJTimeSingleTableView alloc] initWithFrame:CGRectMake(WIDTH, 280, WIDTH, 1700) style:UITableViewStylePlain];
        _groupTableView.isSingle = NO;
    }
    return _groupTableView;
}

@end
